package com.twh.www.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twh.www.entity.WnGoods;
import com.twh.www.service.WnGoodsService;
import com.twh.www.mapper.WnGoodsMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.twh.www.service.RedisService;


@Service
public class WnGoodsServiceImpl extends ServiceImpl<WnGoodsMapper, WnGoods>
    implements WnGoodsService{

    @Autowired
    private RedisService redisService;

    // 查询所有商品
    @Override
    public List<WnGoods> getAllGoods() {
        return this.list();
    }

    // 分页查询所有商品
    @Override
    public Page<WnGoods> getGoodsByPage(int pageNum, int pageSize) {
        // 先查缓存
        List<?> cachedData = redisService.getGoodsPage(pageNum);
        Long total = redisService.getTotal();
        
        if (cachedData != null && total != null) {
            System.out.println("使用缓存：第" + pageNum + "页数据");
            Page<WnGoods> page = new Page<>(pageNum, pageSize);
            page.setRecords((List<WnGoods>) cachedData);
            page.setTotal(total);
            return page;
        }
        
        // 缓存没有，查数据库
        Page<WnGoods> page = new Page<>(pageNum, pageSize);
        this.page(page);
        
        // 存入缓存
        redisService.cacheGoodsPage(pageNum, page.getRecords());
        redisService.cacheTotal(page.getTotal());
        
        return page;
    }

    // 查询热门商品
    @Override
    public List<WnGoods> getHotGoods() {
        return this.lambdaQuery().eq(WnGoods::getHottest, "Y").list();
    }

    @Override
    public boolean save(WnGoods goods) {
        boolean result = super.save(goods);
        if (result) {
            redisService.clearGoodsCache();
        }
        return result;
    }
    // 模糊查询商品并分页
    @Override
    public Page<WnGoods> searchGoodsByPage(String keyword, int pageNum, int pageSize) {
        // 构建查询条件
        QueryWrapper<WnGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", keyword); // 假设商品名称字段为 name

        // 先查缓存
        String cacheKey = "goods:search:" + keyword + ":" + pageNum;
        List<?> cachedData = (List<?>) redisService.redisTemplate.opsForValue().get(cacheKey);
        Long total = redisService.getTotal();

//        System.out.println(cachedData);
        if (cachedData != null && total != null) {
            System.out.println("使用缓存：搜索关键词'" + keyword + "'的第" + pageNum + "页数据");
            Page<WnGoods> page = new Page<>(pageNum, pageSize);
            page.setRecords((List<WnGoods>) cachedData);
            page.setTotal(total);
            return page;
        }

        // 缓存没有，查数据库
        Page<WnGoods> page = new Page<>(pageNum, pageSize);
//        System.out.println("查数据库");
        this.page(page, queryWrapper);

        // 存入缓存
        redisService.cacheSearchGoodsPage(keyword,pageNum, page.getRecords());
        redisService.cacheTotal(page.getTotal());

        return page;
    }
    @Override
    public Page<WnGoods> getGoodsByCategory(int categoryId, int pageNum, int pageSize) {
        QueryWrapper<WnGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        Page<WnGoods> page = new Page<>(pageNum, pageSize);
        this.page(page, queryWrapper);
        return page;
    }

    @Override
    public Page<WnGoods> getadminGoodsByPage(int adminPage, int pagesSize) {
        Page<WnGoods> page = new Page<>(adminPage, pagesSize);
        this.page(page);
        return page;
    }

    @Override
    public boolean updateGoodsStock(Integer id, Integer quantity) {
        WnGoods goods = this.getById(id);
        if (goods != null) {
            goods.setStock(quantity);
            return this.updateById(goods);
        }
        return false;
    }

    @Override
    public boolean deleteGoodsById(Integer id) {
        WnGoods goods = this.getById(id);
        if (goods != null) {
            goods.setStock(0);
            return this.updateById(goods);
        }
        return false;
    }
}




